package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.ZbManage;
import com.ruoyi.business.service.IZbManageService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 指标管理Controller
 * 
 * @author Yuhf
 * @date 2023-11-17
 */
@RestController
@RequestMapping("/business/zbmanage")
public class ZbManageController extends BaseController
{
    @Autowired
    private IZbManageService zbManageService;

    /**
     * 查询指标管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:zbmanage:list')")
    @GetMapping("/list")
    public AjaxResult list(ZbManage zbManage)
    {
        List<ZbManage> list = zbManageService.selectZbManageList(zbManage);
        return success(list);
    }

    /**
     * 导出指标管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:zbmanage:export')")
    @Log(title = "指标管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZbManage zbManage)
    {
        List<ZbManage> list = zbManageService.selectZbManageList(zbManage);
        ExcelUtil<ZbManage> util = new ExcelUtil<ZbManage>(ZbManage.class);
        util.exportExcel(response, list, "指标管理数据");
    }

    /**
     * 获取指标管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:zbmanage:query')")
    @GetMapping(value = "/{zbId}")
    public AjaxResult getInfo(@PathVariable("zbId") Long zbId)
    {
        return success(zbManageService.selectZbManageByZbId(zbId));
    }

    /**
     * 新增指标管理
     */
    @PreAuthorize("@ss.hasPermi('business:zbmanage:add')")
    @Log(title = "指标管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZbManage zbManage)
    {
    	zbManage.setCreateBy(getUsername());
    	zbManage.setCreateTime(DateUtils.getNowDate());
        return toAjax(zbManageService.insertZbManage(zbManage));
    }

    /**
     * 修改指标管理
     */
    @PreAuthorize("@ss.hasPermi('business:zbmanage:edit')")
    @Log(title = "指标管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZbManage zbManage)
    {
    	zbManage.setUpdateBy(getUsername());
    	zbManage.setUpdateTime(DateUtils.getNowDate());
        return toAjax(zbManageService.updateZbManage(zbManage));
    }
    
    /**
     * 复制指标管理
     */
    @PreAuthorize("@ss.hasPermi('business:zbmanage:edit')")
    @Log(title = "指标管理", businessType = BusinessType.UPDATE)
    @GetMapping("/copy/{zbId}")
    public AjaxResult copy(@PathVariable Long zbId)
    {
        return toAjax(zbManageService.copyZbManageByZbId(zbId));
    }
    
    /**
     * 删除指标管理
     */
    @PreAuthorize("@ss.hasPermi('business:zbmanage:remove')")
    @Log(title = "指标管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{zbId}")
    public AjaxResult remove(@PathVariable Long zbId)
    {
    	if (zbManageService.hasChildByZbId(zbId))
        {
            return warn("存在下级指标,不允许删除");
        }
    	ZbManage zbManage = new ZbManage();
    	zbManage.setZbId(zbId);
    	zbManage.setDelTime(DateUtils.getNowDate());
    	zbManage.setDelBy(getUsername());
        return toAjax(zbManageService.deleteZbManageByZbId(zbManage));
    }
}
